/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gui;

import java.awt.Color;
import java.awt.GridBagLayout;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.LineBorder;
import tal.drivechain.chain.AbortChainException;
import tal.drivechain.gui.ForkScrollBarUI;
import tal.drivechain.gui.GUIContainer;
import tal.drivechain.gui.GUIModule;
import tal.drivechain.plugin.Channel;
import tal.drivechain.variable.Variable;

/**
 *
 * @author Tal Eisenberg
 */
public class ScrollBox extends GUIModule implements GUIContainer {
    String id;
    JScrollPane scroll;
    JPanel viewPort;

    @Override
    public void setupProperties() throws IllegalArgumentException, AbortChainException {
        viewPort = new JPanel(); // other modules are added to this panel
        viewPort.setBackground(Color.BLACK);
        component = scroll = new JScrollPane(viewPort);
        JPanel blackPanel = new JPanel();
        blackPanel.setBackground(Color.BLACK);
        scroll.setCorner(JScrollPane.LOWER_TRAILING_CORNER, blackPanel);

        viewPort.setLayout(new GridBagLayout());
        viewPort.addContainerListener(new ContainerListener() {

            public void componentAdded(ContainerEvent e) {
                component.revalidate();
                component.repaint();
            }

            public void componentRemoved(ContainerEvent e) {
                component.revalidate();
                component.repaint();
            }

        });
        super.setupProperties();
    }

    @Override
    public void setColor(Color value) {
        super.setColor(value);
        scroll.getHorizontalScrollBar().setUI(new ForkScrollBarUI(color, color.darker()));
        scroll.getVerticalScrollBar().setUI(new ForkScrollBarUI(color, color.darker()));
        scroll.setBorder(new LineBorder(color.darker(), 1));
    }

    public void setId(String value) {
        if (isRunning())
            throw new IllegalArgumentException("Can't set id while chain is open.");
        if (getParentChain().getSharedObjects().containsValue(viewPort))
            getParentChain().getSharedObjects().remove(id);

        if (value!=null)
            id = (String)value;
        else id = getName();

        getParentChain().getSharedObjects().put(id, viewPort);
    } public String getId() { return id; }

    public void setVertScroll(Boolean b) {
        if (b)
            scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        else scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
    } public boolean getVertScroll() { return (scroll.getVerticalScrollBarPolicy()==ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); }

    public void setHorScroll(Boolean b) {
        if (b)
            scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        else scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    } public boolean getHorScroll() { return (scroll.getHorizontalScrollBarPolicy()==ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); }

    @Override
    protected void msgReceived(Variable in, long timestamp, Channel channel) {}

    @Override
    public void destroy() {
        super.destroy();
        getParentChain().getSharedObjects().remove(id);
    }

}
